package xyz.kongbai121.utils;

import java.io.Serializable;

/**
 * @author ljq
 * @version 1.0.0
 * @Description
 * @createTime 2021年11月05日 11:12:00
 */
public final class Result<D> implements Serializable {
    private boolean success;
    private int code;
    private String msg;
    private D data;

    public Result() {
    }

    private Result(boolean success, int code, String msg, D data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static Result ok() {
        return ok(null);
    }

    public static Result ok(String msg) {
        return ok(msg, null);
    }

    public static <D> Result<D> ok(D data) {
        return ok(null, data);
    }

    public static <D> Result<D> ok(String msg, D data) {
        return newInstance(true, 200, msg, data);
    }


    public static Result error() {
        return error(ResultCode.FAIL);
    }

    public static Result error(ResultCode errorCode) {
        return error(errorCode, errorCode.getDevelopDetail());
    }

    public static Result error(String errorMsg) {
        return error(ResultCode.FAIL, errorMsg);
    }

    public static Result error(ResultCode errorCode, String errorMsg) {
        return error(errorCode, errorMsg, null);
    }

    public static <D> Result<D> error(ResultCode errorCode, String errorMsg, D data) {
        return newInstance(false, errorCode.getDevelopCode(), errorMsg, data);
    }


    public static Result error(int errorCode) {
        return error(errorCode, null);
    }

    public static Result error(int errorCode, String errorMsg) {
        return error(errorCode, errorMsg, null);
    }

    public static <D> Result<D> error(int errorCode, String errorMsg, D data) {
        return newInstance(false, errorCode, errorMsg, data);
    }


    private static <D> Result<D> newInstance(boolean isSuccess, int code, String msg, D data) {
        return new Result(isSuccess, code, msg, data);
    }


    public boolean isSuccess() {
        return success;
    }

    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }

    public D getData() {
        return data;
    }
}
